/*
 * @Author: liu
 * @Date: 2024-04-21 00:37:33
 * @LastEditors: liu
 * @LastEditTime: 2024-05-04 11:44:18
 * @FilePath: \yyx_ser_ts\app\service\CourseService.ts
 * @Description:课程相关服务
 * 天道酬勤
 */
import {
  ChapterException,
  GetCourseException,
  LessonException
} from '../exception/HttpException'
import Chapter from '../model/Chapter'
import Course from '../model/Course'
import Lesson from '../model/Lesson'
import { LessonResInfo } from '../types/custom'

//通过ID查找单门课程信息
class CourseService {
  //保存课程信息方法
  async saveCourseInfoSer(courseInfo: any) {
    const res = Course.create(courseInfo)
    return (await res).dataValues
  }

  //通过ID查找单门课程信息
  async findCourseInfoById(id: number) {
    const res = await Course.findByPk(id)
    if (res) {
      return res
    } else {
      throw new GetCourseException('', '获取课程信息失败，课程ID不存在!')
    }
  }

  //查找所有课程信息方法
  async findAllCourses(pageNum: number, pageSize: number) {
    // //1.获取总数
    // const count = await Course.count()
    // //2.获取分页具体数据
    // const offset = (pageNum-1).pageSize
    // const rows = await Course.findAll({offset:offset,limit:pageSize*1})

    //优化
    const offset = (pageNum - 1) * pageSize
    //
    const { count, rows } = await Course.findAndCountAll({
      offset: offset,
      limit: pageSize * 1,
      order: [['id', 'DESC']]
    })

    if (rows.length !== 0) {
      return {
        pageNum,
        pageSize,
        total: count,
        list: rows
      }
    } else {
      throw new GetCourseException('', '获取课程列表失败，课程列表为空!')
    }
  }

  //查找所有章节信息
  async findAllChapter() {
    const res = await Chapter.findAll({
      attributes: ['course_id', 'chapterName']
    })
    return res
  }

  //通过课程ID获取章节信息
  async findChapterById(id: number) {
    const res = await Chapter.findByPk(id, {
      attributes: ['id', 'course_id', 'chapterName', 'description', 'sort']
    })

    if (res) {
      return res
    } else {
      throw new ChapterException('', '获取章节信息失败，课程ID不存在!')
    }
  }

  // 通过课时ID获取课时信息
  async findLessonById(id: number) {
    const res = await Lesson.findByPk(id, {
      attributes: [
        'id',
        'course_id',
        'course_id',
        'LessonName',
        'brief',
        'sort',
        'status'
      ]
    })

    if (res) {
      return res
    } else {
      return -1
      //throw new LessonException('', '获取课时信息失败，课时ID不存在!')
    }
  }

  //关联测试
  async findChAndLeByCourseId(courseId: number) {
    const res = await Chapter.findAll({
      where: {
        course_id: courseId
      },
      include: [Lesson]
    })
    if (res) {
      return res
    } else {
      throw new ChapterException('', '获取章节信息失败，课程ID不存在!')
    }
  }

  //关联测试
  async test(courseId: number) {
    //  const res = await Video.findAll()
    // return res
    // const res = await Chapter.findOne({
    //   include: [Video]
    // })

    console.log(courseId)

    const res = await Chapter.findAll({
      where: {
        course_id: courseId
      },
      include: [Lesson]
    })
    if (res) {
      return res
    } else {
      throw new ChapterException('', '获取章节信息失败，课程ID不存在!')
    }
  }

  //新增，更新章节信息
  async updateChapter(chapterInfo: any) {
    const { id } = chapterInfo
    console.log(id)

    if (id) {
      const res = await Chapter.update(chapterInfo, { where: { id } })
      console.log(res)
      return res[0] > 0 ? true : false
    } else {
      const res = await Chapter.create(chapterInfo)
      return res
    }
  }

  //新增，更新课时信息
  async updateLesson(lessonInfo: any) {
    const { id } = lessonInfo
    console.log(id)

    //如果id存在，说明是更新如果成功返回true，否则返回失败
    if (id) {
      const res = await Lesson.update(lessonInfo, { where: { id } })
      console.log(res, '更新')
      return res[0] > 0 ? true : false
    } else {
      const res = await Lesson.create(lessonInfo)
      // console.log(res, 'create')
      return res
    }
  }

  //更新章节状态
  async updateChapterStatus(id: number, status: number) {
    return await Chapter.update(
      { status },
      {
        where: { id }
      }
    )
  }

  //删除课时
  async deleteLesson(id: number) {
    return Lesson.destroy({
      where: { id }
    })
  }

  //删除单个课程
  async deleteCourse(id: number) {
    return Course.destroy({
      where: { id }
    })
  }

  //删除多个课程
  async deleteCourses(listData: []) {
    // console.log(listData)
    return Course.destroy({
      where: {
        id: listData
      }
    })
  }
}

export default new CourseService()
